* These are the estimates presented in Figure 2 *

***************************************************
** YEAR t-2
***************************************************
cd "/Users/mariadelmarcanizaresespadafor/Dropbox/Side projects/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Data"
*cd "~/Dropbox/EUI/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Data"

use "gambling_data.dta", clear
merge m:1 apuestas_id using "dapuestas_year.dta"

gen treatment=0
replace treatment=1 if metres<500 & year<year_lic & publico==2 

gen treatment_all=0
replace treatment_all=1 if metres<500 & year+2>=year_lic


* This is to manually get data 'wide'
drop num
gen num=.
replace num= n18 if year==2017
replace num= n16 if year==2016
replace num= n15 if year==2015
replace num= n14 if year==2014


drop pau
gen pau=.
replace pau= pau1516 if year==2015
replace pau= pau16 if year==2016
replace pau= pau17 if year==2017
replace pau= pau14 if year==2014

* Income by district: 
drop renta
gen renta = .
replace renta = 31 if distrito==4
replace renta = 40 if distrito==1
replace renta = 51 if distrito==14
replace renta = 53 if distrito==15
replace renta = 60 if distrito==5
replace renta = 34 if distrito==17
replace renta = 48 if distrito==6
replace renta = 48 if distrito==8
replace renta = 56 if distrito==11
replace renta = 30 if distrito==10
replace renta = 27 if distrito==3
replace renta = 25 if distrito==18
replace renta = 24 if distrito==13
replace renta = 34 if distrito==12
replace renta = 36 if distrito==7
replace renta = 47 if distrito==9
replace renta = 26 if distrito==21
replace renta = 30 if distrito==20
replace renta = 31 if distrito==19
replace renta = 34 if distrito==16
replace renta = 46 if distrito==2

* Dummy variable for income: Districts with incomes higher to the median==1
drop renta2
gen renta2 = 0 
replace renta2 = 1 if renta>40


cd "/Users/mariadelmarcanizaresespadafor/Dropbox/Side projects/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Estimates"
*cd "~/Dropbox/EUI/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Estimates"

** Publico=1 -> charter schools
** Publico=2 -> public schools

** Renta2=0 -> poorer 
** Renta2=1 -> richer

drop treat
gen treat=treatment_all

***********************
***********************

*** This makes stata understand data as a panel. And, when applying the xtreg command, adding fixed effects at the observation level. 

xtset school_id year

***********************
***********************

xtreg pau treat i.year, fe
regsave treat using "pau_3.dta", tstat pval ci level(90) replace addlabel (Outcome, pau,Income, All, School, All, Year, -2, Level, 90)

xtreg pau treat i.year, fe
regsave treat using "pau_3.dta", tstat pval ci level(95) append addlabel (Outcome, pau,Income, All, School, All, Year, -2, Level, 95)


*** Charter

xtreg pau treat i.year if publico==1, fe
regsave treat using "pau_3.dta", tstat pval ci level(90) append addlabel (Outcome, pau,Income, All, School, Charter, Year, -2, Level, 90)

xtreg pau treat i.year if publico==1, fe
regsave treat using "pau_3.dta", tstat pval ci level(95) append addlabel (Outcome, pau,Income, All, School, Charter, Year, -2, Level, 95)

***Public

xtreg pau treat i.year if publico==2, fe
regsave treat using "pau_3.dta", tstat pval ci level(90) append addlabel (Outcome, pau,Income, All, School, Public, Year, -2, Level, 90)

xtreg pau treat i.year if publico==2, fe
regsave treat using "pau_3.dta", tstat pval ci level(95) append  addlabel (Outcome, pau,Income, All, School, Public, Year, -2, Level, 95)

*** Charter, poor

xtreg pau treat i.year if publico==1 & renta2==0, fe
regsave treat using "pau_3.dta", tstat pval ci level(90) append addlabel (Outcome, pau,Income, Poor, School, Charter, Year, -2, Level, 90)

xtreg pau treat i.year if publico==1 & renta2==0, fe
regsave treat using "pau_3.dta", tstat pval ci level(95) append addlabel (Outcome, pau,Income, Poor, School, Charter, Year, -2, Level, 95)

*** Charter, rich: We don't get any estimate for lack of observations:

*xtreg pau treat i.year if publico==1 & renta2==1, fe
*regsave treat using "pau_3.dta", tstat pval ci level(90) append addlabel (Outcome, pau,Income, Rich, School, Charter, Year, 1, Level, 90)

*xtreg pau treat i.year if publico==1 & renta2==1, fe
*regsave treat using "pau_3.dta", tstat pval ci level(95) append addlabel (Outcome, pau,Income, Rich, School, Charter, Year, 1, Level, 95)

*** Public, poor

xtreg pau treat i.year if publico==2 & renta2==0, fe
regsave treat using "pau_3.dta", tstat pval ci level(90) append addlabel (Outcome, pau,Income, Poor, School, Public, Year, -2, Level, 90)

xtreg pau treat i.year if publico==2 & renta2==0, fe
regsave treat using "pau_3.dta", tstat pval ci level(95) append addlabel (Outcome, pau, Income, Poor, School, Public, Year, -2, Level, 95)

*** Public, rich

xtreg pau treat i.year if publico==2 & renta2==1, fe
regsave treat using "pau_3.dta", tstat pval ci level(90) append addlabel (Outcome, pau,Income, Rich, School, Public, Year, -2, Level, 90)

xtreg pau treat i.year if publico==2 & renta2==1, fe
regsave treat using "pau_3.dta", tstat pval ci level(95) append addlabel (Outcome, pau, Income, Rich, School, Public, Year, -2, Level, 95)

********************************************************
* Year t-1
********************************************************

cd "/Users/mariadelmarcanizaresespadafor/Dropbox/Side projects/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Data"
*cd "~/Dropbox/EUI/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Data"

use "gambling_data.dta", clear
merge m:1 apuestas_id using "dapuestas_year.dta"

gen treatment=0
replace treatment=1 if metres<500 & year<year_lic & publico==2 

gen treatment_all=0
replace treatment_all=1 if metres<500 & year+1>=year_lic


* This is to manually get data 'wide'
drop num 
gen num=.
replace num= n18 if year==2017
replace num= n16 if year==2016
replace num= n15 if year==2015
replace num= n14 if year==2014

drop pau
gen pau=.
replace pau= pau1516 if year==2015
replace pau= pau16 if year==2016
replace pau= pau17 if year==2017
replace pau= pau14 if year==2014

* Income by district: 
drop renta
gen renta = .
replace renta = 31 if distrito==4
replace renta = 40 if distrito==1
replace renta = 51 if distrito==14
replace renta = 53 if distrito==15
replace renta = 60 if distrito==5
replace renta = 34 if distrito==17
replace renta = 48 if distrito==6
replace renta = 48 if distrito==8
replace renta = 56 if distrito==11
replace renta = 30 if distrito==10
replace renta = 27 if distrito==3
replace renta = 25 if distrito==18
replace renta = 24 if distrito==13
replace renta = 34 if distrito==12
replace renta = 36 if distrito==7
replace renta = 47 if distrito==9
replace renta = 26 if distrito==21
replace renta = 30 if distrito==20
replace renta = 31 if distrito==19
replace renta = 34 if distrito==16
replace renta = 46 if distrito==2

* Dummy variable for income: Districts with incomes higher to the median==1
drop renta2
gen renta2 = 0 
replace renta2 = 1 if renta>40

cd "/Users/mariadelmarcanizaresespadafor/Dropbox/Side projects/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Estimates"
*cd "~/Dropbox/EUI/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Estimates"

** Publico=1 -> charter schools
** Publico=2 -> public schools

** Renta2=0 -> poorer 
** Renta2=1 -> richer

drop treat
gen treat=treatment_all

***********************
***********************

*** This makes stata understand data as a panel. And, when applying the xtreg command, adding fixed effects at the observation level. 

xtset school_id year

***********************
***********************

xtreg pau treat i.year, fe
regsave treat using "pau_1.dta", tstat pval ci level(90) replace addlabel (Outcome, pau,Income, All, School, All, Year, -1, Level, 90)

xtreg pau treat i.year, fe
regsave treat using "pau_1.dta", tstat pval ci level(95) append addlabel (Outcome, pau,Income, All, School, All, Year, -1, Level, 95)


*** Charter

xtreg pau treat i.year if publico==1, fe
regsave treat using "pau_1.dta", tstat pval ci level(90) append addlabel (Outcome, pau,Income, All, School, Charter, Year, -1, Level, 90)

xtreg pau treat i.year if publico==1, fe
regsave treat using "pau_1.dta", tstat pval ci level(95) append addlabel (Outcome, pau,Income, All, School, Charter, Year, -1, Level, 95)

***Public

xtreg pau treat i.year if publico==2, fe
regsave treat using "pau_1.dta", tstat pval ci level(90) append addlabel (Outcome, pau,Income, All, School, Public, Year, -1, Level, 90)

xtreg pau treat i.year if publico==2, fe
regsave treat using "pau_1.dta", tstat pval ci level(95) append  addlabel (Outcome, pau,Income, All, School, Public, Year, -1, Level, 95)

*** Charter, poor

xtreg pau treat i.year if publico==1 & renta2==0, fe
regsave treat using "pau_1.dta", tstat pval ci level(90) append addlabel (Outcome, pau,Income, Poor, School, Charter, Year, -1, Level, 90)

xtreg pau treat i.year if publico==1 & renta2==0, fe
regsave treat using "pau_1.dta", tstat pval ci level(95) append addlabel (Outcome, pau,Income, Poor, School, Charter, Year, -1, Level, 95)

*** Charter, rich

*xtreg pau treat i.year if publico==1 & renta2==1, fe
*regsave treat using "pau_1.dta", tstat pval ci level(90) append addlabel (Outcome, pau,Income, Rich, School, Charter, Year, 1, Level, 90)

*xtreg pau treat i.year if publico==1 & renta2==1, fe
*regsave treat using "pau_1.dta", tstat pval ci level(95) append addlabel (Outcome, pau,Income, Rich, School, Charter, Year, 1, Level, 95)

*** Public, poor

xtreg pau treat i.year if publico==2 & renta2==0, fe
regsave treat using "pau_1.dta", tstat pval ci level(90) append addlabel (Outcome, pau,Income, Poor, School, Public, Year, -1, Level, 90)

xtreg pau treat i.year if publico==2 & renta2==0, fe
regsave treat using "pau_1.dta", tstat pval ci level(95) append addlabel (Outcome, pau, Income, Poor, School, Public, Year, -1, Level, 95)

*** Public, rich

xtreg pau treat i.year if publico==2 & renta2==1, fe
regsave treat using "pau_1.dta", tstat pval ci level(90) append addlabel (Outcome, pau,Income, Rich, School, Public, Year, -1, Level, 90)

xtreg pau treat i.year if publico==2 & renta2==1, fe
regsave treat using "pau_1.dta", tstat pval ci level(95) append addlabel (Outcome, pau, Income, Rich, School, Public, Year, -1, Level, 95)

**********************************************
** Year 0
***********************************************


*cd "~/Dropbox/EUI/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Data"
cd "/Users/mariadelmarcanizaresespadafor/Dropbox/Side projects/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Data"

use "gambling_data.dta", clear
merge m:1 apuestas_id using "dapuestas_year.dta"

gen treatment=0
replace treatment=1 if metres<500 & year>=year_lic & publico==2 

gen treatment_all=0
replace treatment_all=1 if metres<500 & year>=year_lic

drop num 
gen num=.
replace num= n18 if year==2017
replace num= n16 if year==2016
replace num= n15 if year==2015
replace num= n14 if year==2014

drop pau
gen pau=.
replace pau= pau1516 if year==2015
replace pau= pau16 if year==2016
replace pau= pau17 if year==2017
replace pau= pau14 if year==2014

* Income by district: 
drop renta
gen renta = .
replace renta = 31 if distrito==4
replace renta = 40 if distrito==1
replace renta = 51 if distrito==14
replace renta = 53 if distrito==15
replace renta = 60 if distrito==5
replace renta = 34 if distrito==17
replace renta = 48 if distrito==6
replace renta = 48 if distrito==8
replace renta = 56 if distrito==11
replace renta = 30 if distrito==10
replace renta = 27 if distrito==3
replace renta = 25 if distrito==18
replace renta = 24 if distrito==13
replace renta = 34 if distrito==12
replace renta = 36 if distrito==7
replace renta = 47 if distrito==9
replace renta = 26 if distrito==21
replace renta = 30 if distrito==20
replace renta = 31 if distrito==19
replace renta = 34 if distrito==16
replace renta = 46 if distrito==2

* Dummy variable for income: Districts with incomes higher to the median==1
drop renta2
gen renta2 = 0 
replace renta2 = 1 if renta>40


*cd "~/Dropbox/EUI/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Estimates"
cd "/Users/mariadelmarcanizaresespadafor/Dropbox/Side projects/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Estimates"
** Publico=1 -> charter schools
** Publico=2 -> public schools

** Renta2=0 -> poorer 
** Renta2=1 -> richer

drop treat
gen treat=treatment_all

***********************
***********************

*** This makes stata understand data as a panel. And, when applying the xtreg command, adding fixed effects at the observation level. 

xtset school_id year

***********************
***********************

xtreg pau treat i.year, fe
regsave treat using "pau0.dta", tstat pval ci level(90) replace addlabel (Outcome, pau,Income, All, School, All, Year, 0, Level, 90)

xtreg pau treat i.year, fe
regsave treat using "pau0.dta", tstat pval ci level(95) append addlabel (Outcome, pau,Income, All, School, All, Year, 0, Level, 95)


*** Charter

xtreg pau treat i.year if publico==1, fe
regsave treat using "pau0.dta", tstat pval ci level(90) append addlabel (Outcome, pau,Income, All, School, Charter, Year, 0, Level, 90)

xtreg pau treat i.year if publico==1, fe
regsave treat using "pau0.dta", tstat pval ci level(95) append addlabel (Outcome, pau,Income, All, School, Charter, Year, 0, Level, 95)

***Public

xtreg pau treat i.year if publico==2, fe
regsave treat using "pau0.dta", tstat pval ci level(90) append addlabel (Outcome, pau,Income, All, School, Public, Year, 0, Level, 90)

xtreg pau treat i.year if publico==2, fe
regsave treat using "pau0.dta", tstat pval ci level(95) append  addlabel (Outcome, pau,Income, All, School, Public, Year, 0, Level, 95)

*** Charter, poor

xtreg pau treat i.year if publico==1 & renta2==0, fe
regsave treat using "pau0.dta", tstat pval ci level(90) append addlabel (Outcome, pau,Income, Poor, School, Charter, Year, 0, Level, 90)

xtreg pau treat i.year if publico==1 & renta2==0, fe
regsave treat using "pau0.dta", tstat pval ci level(95) append addlabel (Outcome, pau,Income, Poor, School, Charter, Year, 0, Level, 95)

*** Charter, rich

*xtreg pau treat i.year if publico==1 & renta2==1, fe
*regsave treat using "pau0.dta", tstat pval ci level(90) append addlabel (Outcome, pau,Income, Rich, School, Charter, Year, 0, Level, 90)

*xtreg pau treat i.year if publico==1 & renta2==1, fe
*regsave treat using "pau0.dta", tstat pval ci level(95) append addlabel (Outcome, pau,Income, Rich, School, Charter, Year, 0, Level, 95)

*** Public, poor

xtreg pau treat i.year if publico==2 & renta2==0, fe
regsave treat using "pau0.dta", tstat pval ci level(90) append addlabel (Outcome, pau,Income, Poor, School, Public, Year, 0, Level, 90)

xtreg pau treat i.year if publico==2 & renta2==0, fe
regsave treat using "pau0.dta", tstat pval ci level(95) append addlabel (Outcome, pau, Income, Poor, School, Public, Year, 0, Level, 95)

*** Public, rich

xtreg pau treat i.year if publico==2 & renta2==1, fe
regsave treat using "pau0.dta", tstat pval ci level(90) append addlabel (Outcome, pau,Income, Rich, School, Public, Year, 0, Level, 90)

xtreg pau treat i.year if publico==2 & renta2==1, fe
regsave treat using "pau0.dta", tstat pval ci level(95) append addlabel (Outcome, pau, Income, Rich, School, Public, Year, 0, Level, 95)

*************************************************
*** YEAR t+1 
**************************************************
*cd "~/Dropbox/EUI/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Data"
cd "/Users/mariadelmarcanizaresespadafor/Dropbox/Side projects/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Data"

use "gambling_data.dta", clear
merge m:1 apuestas_id using "dapuestas_year.dta"

gen treatment=0
replace treatment=1 if metres<500 & year>year_lic & publico==2 

gen treatment_all=0
replace treatment_all=1 if metres<500 & year>year_lic

drop num 
gen num=.
replace num= n18 if year==2017
replace num= n16 if year==2016
replace num= n15 if year==2015
replace num= n14 if year==2014

drop pau
gen pau=.
replace pau= pau1516 if year==2015
replace pau= pau16 if year==2016
replace pau= pau17 if year==2017
replace pau= pau14 if year==2014

* Income by district: 
drop renta
gen renta = .
replace renta = 31 if distrito==4
replace renta = 40 if distrito==1
replace renta = 51 if distrito==14
replace renta = 53 if distrito==15
replace renta = 60 if distrito==5
replace renta = 34 if distrito==17
replace renta = 48 if distrito==6
replace renta = 48 if distrito==8
replace renta = 56 if distrito==11
replace renta = 30 if distrito==10
replace renta = 27 if distrito==3
replace renta = 25 if distrito==18
replace renta = 24 if distrito==13
replace renta = 34 if distrito==12
replace renta = 36 if distrito==7
replace renta = 47 if distrito==9
replace renta = 26 if distrito==21
replace renta = 30 if distrito==20
replace renta = 31 if distrito==19
replace renta = 34 if distrito==16
replace renta = 46 if distrito==2

* Dummy variable for income: Districts with incomes higher to the median==1
drop renta2
gen renta2 = 0 
replace renta2 = 1 if renta>40

*cd "~/Dropbox/EUI/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Estimates"
cd "/Users/mariadelmarcanizaresespadafor/Dropbox/Side projects/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Estimates"

** Publico=1 -> charter schools
** Publico=2 -> public schools

** Renta2=0 -> poorer 
** Renta2=1 -> richer

drop treat
gen treat=treatment_all

***********************
***********************

*** This makes stata understand data as a panel. And, when applying the xtreg command, adding fixed effects at the observation level. 

xtset school_id year

***********************
***********************

xtreg pau treat i.year, fe
regsave treat using "pau1.dta", tstat pval ci level(90) replace addlabel (Outcome, pau,Income, All, School, All, Year, 1, Level, 90)

xtreg pau treat i.year, fe
regsave treat using "pau1.dta", tstat pval ci level(95) append addlabel (Outcome, pau,Income, All, School, All, Year, 1, Level, 95)


*** Charter

xtreg pau treat i.year if publico==1, fe
regsave treat using "pau1.dta", tstat pval ci level(90) append addlabel (Outcome, pau,Income, All, School, Charter, Year, 1, Level, 90)

xtreg pau treat i.year if publico==1, fe
regsave treat using "pau1.dta", tstat pval ci level(95) append addlabel (Outcome, pau,Income, All, School, Charter, Year, 1, Level, 95)

***Public

xtreg pau treat i.year if publico==2, fe
regsave treat using "pau1.dta", tstat pval ci level(90) append addlabel (Outcome, pau,Income, All, School, Public, Year, 1, Level, 90)

xtreg pau treat i.year if publico==2, fe
regsave treat using "pau1.dta", tstat pval ci level(95) append  addlabel (Outcome, pau,Income, All, School, Public, Year, 1, Level, 95)

*** Charter, poor

xtreg pau treat i.year if publico==1 & renta2==0, fe
regsave treat using "pau1.dta", tstat pval ci level(90) append addlabel (Outcome, pau,Income, Poor, School, Charter, Year, 1, Level, 90)

xtreg pau treat i.year if publico==1 & renta2==0, fe
regsave treat using "pau1.dta", tstat pval ci level(95) append addlabel (Outcome, pau,Income, Poor, School, Charter, Year, 1, Level, 95)

*** Charter, rich

*xtreg pau treat i.year if publico==1 & renta2==1, fe
*regsave treat using "pau1.dta", tstat pval ci level(90) append addlabel (Outcome, pau,Income, Rich, School, Charter, Year, 1, Level, 90)

*xtreg pau treat i.year if publico==1 & renta2==1, fe
*regsave treat using "pau1.dta", tstat pval ci level(95) append addlabel (Outcome, pau,Income, Rich, School, Charter, Year, 1, Level, 95)

*** Public, poor

xtreg pau treat i.year if publico==2 & renta2==0, fe
regsave treat using "pau1.dta", tstat pval ci level(90) append addlabel (Outcome, pau,Income, Poor, School, Public, Year, 1, Level, 90)

xtreg pau treat i.year if publico==2 & renta2==0, fe
regsave treat using "pau1.dta", tstat pval ci level(95) append addlabel (Outcome, pau, Income, Poor, School, Public, Year, 1, Level, 95)

*** Public, rich

xtreg pau treat i.year if publico==2 & renta2==1, fe
regsave treat using "pau1.dta", tstat pval ci level(90) append addlabel (Outcome, pau,Income, Rich, School, Public, Year, 1, Level, 90)

xtreg pau treat i.year if publico==2 & renta2==1, fe
regsave treat using "pau1.dta", tstat pval ci level(95) append addlabel (Outcome, pau, Income, Rich, School, Public, Year, 1, Level, 95)


******
******

*cd "~/Dropbox/EUI/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Estimates"
cd "/Users/mariadelmarcanizaresespadafor/Dropbox/Side projects/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Estimates"

use "pau1.dta", clear 

append using "pau0.dta"

append using "pau_1.dta"

append using "pau_3.dta"

replace Year=Year+2

gen ci_lower90 = ci_lower if Level==90
gen ci_upper90 = ci_upper if Level==90
gen ci_lower95 = ci_lower if Level==95
gen ci_upper95 = ci_upper if Level==95

gen out = 0 if Outcome=="pau"
replace out = 1 if Outcome=="papto" 

gen inc = 0 if Income=="Poor"
replace inc = 1 if Income=="All" 
replace inc = 2 if Income=="Rich" 

gen charter = 0 if School=="Charter"
replace charter = 1 if School=="Public"
replace charter = 2 if School=="All"

save "pau2.dta", replace 
